package com.manxi.mapper;

import com.github.pagehelper.Page;
import com.manxi.annotation.AutoFill;
import com.manxi.dto.EmployeePageQueryDTO;
import com.manxi.entity.Employee;
import com.manxi.enumeration.OperationType;
import org.apache.ibatis.annotations.*;

@Mapper
public interface EmployeeMapper {

    /**
     * 根据用户名查询员工
     *
     * @param username
     * @return
     */
    @Select("select * from employee where username = #{username}")
    Employee getByUsername(String username);


    /**
     * 新增员工
     *
     * @param employee
     */
    @Insert("insert into employee (name, username, password, phone, sex, id_number, create_time, update_time, create_user, update_user,status)" +
            "values (#{name},#{username},#{password},#{phone},#{sex},#{idNumber},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})")
    @AutoFill(value = OperationType.INSERT)
    void insert(Employee employee);

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @Select("<script> " +
            "select * from employee " +
            "where 1= 1 " +
            "<if test='name != null'> AND name like CONCAT('%',#{name},'%')</if> " +
            "order by create_time desc " +
            "</script>")
    Page<Employee> pageQuery(EmployeePageQueryDTO employeePageQueryDTO);

    /**
     * 更改员工信息（或启用禁用））
     *
     * @param employee
     */
    @Update("<script> " +
            "update employee set " +
            "<if test='name != null'> name = #{name}, </if> " +
            "<if test='username != null'> username = #{username}, </if> " +
            "<if test='password != null'> password = #{password}, </if> " +
            "<if test='phone != null'> phone = #{phone}, </if> " +
            "<if test='sex != null'> sex = #{sex}, </if> " +
            "<if test='idNumber != null'> id_number = #{idNumber}, </if> " +
            "<if test='updateTime != null'> update_time = #{updateTime}, </if> " +
            "<if test='updateUser != null'> update_user = #{updateUser}, </if> " +
            "<if test='status != null'> status = #{status} </if>" +
            "where id = #{id} " +
            "</script>")
    @AutoFill(value = OperationType.UPDATE)
    void updateEmployee(Employee employee);

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @Select("select * from employee where id = #{id}")
    Employee getById(long id);

    /**
     * 根据id修改密码
     *
     * @param id
     * @return
     */
    @Update("update employee set password = #{newPassword} where id = #{id}")
    void editPassword(Long id, String newPassword);

    /**
     * 根据id删除员工
     *
     * @param id
     * @return
     */
    @Delete("delete from employee where id = #{id}")
    void deleteById(Long id);


}
